var _api2 = _interopRequireDefault(require("../../utils/api")), _user = _interopRequireDefault(require("../../utils/user")), _util2 = _interopRequireDefault(require("../../../utils/util")), _eventEmitter = _interopRequireDefault(require("../../utils/event-emitter")), _withinmsg = _interopRequireDefault(require("../../utils/withinmsg"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _objectSpread(e) {
    for (var t = 1; t < arguments.length; t++) {
        var a = null != arguments[t] ? arguments[t] : {}, i = Object.keys(a);
        "function" == typeof Object.getOwnPropertySymbols && (i = i.concat(Object.getOwnPropertySymbols(a).filter(function(t) {
            return Object.getOwnPropertyDescriptor(a, t).enumerable;
        }))), i.forEach(function(t) {
            _defineProperty(e, t, a[t]);
        });
    }
    return e;
}

function _defineProperty(t, e, a) {
    return e in t ? Object.defineProperty(t, e, {
        value: a,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = a, t;
}

function _toConsumableArray(t) {
    return _arrayWithoutHoles(t) || _iterableToArray(t) || _nonIterableSpread();
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _iterableToArray(t) {
    if (Symbol.iterator in Object(t) || "[object Arguments]" === Object.prototype.toString.call(t)) return Array.from(t);
}

function _arrayWithoutHoles(t) {
    if (Array.isArray(t)) {
        for (var e = 0, a = new Array(t.length); e < t.length; e++) a[e] = t[e];
        return a;
    }
}

var hideTotalMsgModal, globalData = getApp().globalData, POSITION = [ {
    top: -2,
    right: -5
}, {
    top: -5,
    right: -8
}, {
    top: -8,
    right: -18
} ], maxCount = 99, initReads = !1, id = 0, register = {}, duration = 5e3;

Component({
    properties: {
        styles: {
            type: String,
            value: ""
        },
        name: String,
        uid: String,
        avatar: String,
        card: Object,
        toCard: Object,
        showTotalMsgModal: {
            type: Boolean,
            value: !0
        },
        report: String
    },
    data: {
        notifyShow: !1,
        totalMsgShown: !1,
        msgContent: {}
    },
    attached: function() {
        hideTotalMsgModal || (hideTotalMsgModal = !this.data.showTotalMsgModal), this.setData({
            hideTotalMsgModal: hideTotalMsgModal
        });
    },
    ready: function() {
        this._id = id++, (register["".concat(this._id)] = this).__getUnreads(), this.initEvent();
    },
    detached: function() {
        delete register["".concat(this._id)];
    },
    methods: {
        initEvent: function() {
            [ [ "radar.message.socket", this.__handleSocketMsg.bind(this) ], [ "radar.unreads.change", this.__getUnreads.bind(this) ] ].forEach(function(t) {
                return _eventEmitter.default.addListener.apply(_eventEmitter.default, _toConsumableArray(t));
            });
        },
        __handleSocketMsg: function(t) {
            var e = getCurrentPages(), a = e[e.length - 1].route;
            /(withinmsg|minichat)$/.test(a) || this.__marketRadarChat(t);
        },
        __marketRadarChat: function(t) {
            var e = this;
            +t.issystem || this.data.totalMsgShown || (this.setData({
                msgContent: {
                    avatar: t.avatar,
                    realname: t.realname,
                    chatid: t.chatid,
                    uid: t.uid,
                    content: t.content
                }
            }, function() {
                return e.setData({
                    notifyShow: !0
                });
            }), this.__startTimer());
        },
        __startTimer: function() {
            var t = this;
            clearTimeout(this.notifyTimer), this.notifyTimer = wx.setTimeout(function() {
                return t._onCancel();
            }, duration);
        },
        _onCancel: function() {
            clearTimeout(this.notifyTimer), Object.keys(register).forEach(function(t) {
                register[t].setData({
                    notifyShow: !1
                });
            });
        },
        __getUnreads: function() {
            var t = globalData.radarCount;
            void 0 !== t && (t = t || 0, initReads ? this.__setCount(t) : (this.__handleCount(t, !0), 
            initReads = !0));
        },
        __handleCount: function(t, e) {
            t = isNaN(+t) ? 0 : +t, t = Math.max(0, t), e && 0 < t && this.data.showTotalMsgModal && Object.keys(register).forEach(function(t) {
                register[t].setData({
                    totalMsgShown: !0
                });
            }), globalData.radarCount = t, this.__setCount(t), this.checkWithAniClose(id), hideTotalMsgModal = !0;
        },
        __setCount: function() {
            var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : 0, e = ("" + t).length, a = this.data.toCard, i = {
                position: POSITION[e - 1],
                count: maxCount < t ? "99+" : t
            };
            a && a.cardid && (i.position = {
                top: 10,
                right: 0
            }), Object.keys(register).forEach(function(t) {
                register[t].setData(i);
            });
        },
        __orient: function() {
            var t = this.data, e = t.report, a = t.notifyShow, i = t.toCard;
            e && this.$report(e);
            var n = _user.default.getTokensSync();
            return n.accesstoken ? a ? this._gotoNotifyChat() : i && +i.uid != +n.uid ? this.__goChat() : void this.gotoWithInMsg() : this.triggerEvent("fail", {
                errMsg: "navigateTo:fail auth deny"
            }, {});
        },
        _gotoNotifyChat: function() {
            var t = this.data.msgContent, e = t.realname, a = t.chatid, i = t.uid;
            this._onCancel(), this.$route("/publicPackage/salesradar/minichat/minichat", {
                chatid: a,
                view: 1,
                touid: i,
                origin: "notification",
                name: encodeURIComponent(e)
            });
        },
        gotoWithInMsg: function() {
            var t = this.data.card;
            t && t.guid ? (this.$emit("radar.navbar.change", 1), this.$route("/pages/radar/index/index", "tab")) : this.$route("/pages/tabbar/card/card", "tab");
        },
        handleQueryData: function() {
            var t = this.data, e = t.name, a = t.uid, i = t.card, n = t.toCard, o = i ? i.cardid : "", r = n ? n.cardid : "", s = n ? n.guid : "";
            return {
                name: encodeURIComponent(e),
                touid: a,
                cardid: o,
                tocardid: r,
                toguid: s
            };
        },
        __goChat: function() {
            var a = this, t = this.handleQueryData();
            if (!_util2.default.checkIsToChat(_objectSpread({
                view: 1
            }, t))) {
                var i = t.name, n = t.touid, o = t.cardid, r = t.tocardid, s = t.toguid;
                if (+_user.default.getTokensSync().uid == +n) return wx.showModal({
                    content: "自己不能跟自己对话哦",
                    showCancel: !1
                });
                _api2.default.chat.chat.create({
                    data: {
                        touid: n
                    }
                }).then(function(t) {
                    var e = t.data.chatid;
                    a.$route("/publicPackage/salesradar/minichat/minichat", {
                        name: i,
                        touid: n,
                        toguid: s,
                        chatid: e,
                        cardid: o,
                        tocardid: r,
                        view: 1
                    });
                });
            }
        },
        __gotoWithinMsgPage: function() {
            this.gotoWithInMsg(), this.__onCloseModal();
        },
        __onCloseModal: function() {
            var t = this;
            Object.keys(register).forEach(function(t) {
                register[t].setData({
                    totalMsgShown: !1
                });
            }), wx.setTimeout(function() {
                return t.setData({
                    hideTotalMsgModal: hideTotalMsgModal
                });
            }, 1200);
        },
        __onReadAll: function() {
            var t = this;
            _api2.default.chat.message.setread().then(function() {
                wx.setTimeout(function() {
                    t.__onCloseModal(), t.__handleCount(0);
                }, 200);
            });
        },
        checkWithAniClose: function(t) {
            var e = this;
            1 === t && wx.setTimeout(function() {
                return e.__onCloseModal();
            }, duration);
        },
        __collectFormId: function(t) {
            var e = t.detail.formId;
            globalData.isDevTools || _api2.default.weapp.collectformid({
                data: {
                    formid: e,
                    app: _withinmsg.default.app
                }
            });
        }
    }
});